/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import bc.PrecioProductoFacadeLocal;
import bc.PrendaFacadeLocal;
import be.ColorPrenda;
import be.EstadoPrenda;
import be.MaterialPrenda;
import be.ModeloPrenda;
import be.PrecioProducto;
import be.Prenda;
import be.TallaPrenda;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 *
 **Copyright  2011 Yury Daniel Zavaleta De la Cruz
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and  limitations under the License.
 */
@ManagedBean
@SessionScoped
public class ManagedBeanPrenda implements Serializable {
    @EJB
    private PrendaFacadeLocal prendaFacade;
    @EJB
    private  PrecioProductoFacadeLocal precioProductoFacade ;
    private Prenda prenda;
    private List<SelectItem> prendasItems = new LinkedList<SelectItem>();
    private HashMap<Integer, Prenda> myprendas = new HashMap<Integer,Prenda>();
private List<Prenda> lista ;

    public ManagedBeanPrenda() {
   prenda = new Prenda();
   lista= new LinkedList<Prenda>();
    }

    public Prenda getPrenda() {
        return prenda;
    }

    public void setPrenda(Prenda prenda) {
        this.prenda = prenda;
    }

     public Prenda getPrenda(Integer id) {
    return (Prenda) myprendas.get(id);
     }

    public HashMap<Integer, Prenda> getMyprendas() {
        return myprendas;
    }

    public void setMyprendas(HashMap<Integer, Prenda> myprendas) {
        this.myprendas = myprendas;
    }

    public List<SelectItem> getPrendasItems() {
          lista = new ArrayList<Prenda>();
        prendasItems = new LinkedList<SelectItem>();
        lista = prendaFacade.findAll();
        for(Prenda p: lista){
        myprendas.put(p.getIdPrenda(), p);
            prendasItems.add(new SelectItem(p, p.getNombrePrenda()));
        }

        return prendasItems;

    }

    public void setPrendasItems(List<SelectItem> prendasItems) {
        this.prendasItems = prendasItems;
    }

    public List<Prenda> getLista_prendas(){
        try {

            lista = prendaFacade.findAll();

        } catch (Exception e) {
        e.printStackTrace();
        }
        return lista;
    }

    public String Nueva(){
    prenda = new Prenda();
prenda.setFechaIngreso(new Date());
    return "nuevo_producto";
    }

     public String edicion(){
return "editar_producto";
    }

    public String Crear_Prenda(){
    prenda.setEstadoPrenda(new EstadoPrenda(1));
    prenda.setFechaIngreso(new Date());
    prenda.setModeloPrenda(new ModeloPrenda(1));
    prenda.setTallaPrenda(new TallaPrenda(1));
    prenda.setColorPrenda(new ColorPrenda(1));
    prenda.setMaterialPrenda(new MaterialPrenda(1));
        try {
            prendaFacade.create(prenda);
/*Agregado aqui*/
            precioProductoFacade.create(new PrecioProducto(prenda.getIdPrenda(), new Date(), BigDecimal.ZERO));

        } catch (Exception e) {
        e.printStackTrace();
        }
    return "productos?faces-redirect=true";
    }

    public String editar_producto(){
        try {
            prendaFacade.edit(prenda);
        } catch (Exception e) {
        e.printStackTrace();
        }
    return "productos?faces-redirect=true";
    }

    public String Volver_Prenda(){
return "index?faces-redirect=true";
     }


 public List<Prenda> completar_filtrado_prenda(String nombre) {
  lista = new LinkedList<Prenda>();
  for(Prenda p : prendaFacade.findAll()){
      myprendas.put(p.getIdPrenda(), p);
    if (p.getNombrePrenda().toUpperCase().indexOf(nombre.toUpperCase())!=-1){
    lista.add(p);

    }
    }
       return lista;
    }

}
